//
//  WeHomePageInterfaceCell.m
//  WeGalaxy
//
//  Created by Zheng on 2017/7/18.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "WeHomePageInterfaceCell.h"

@implementation WeHomePageInterfaceControl

- (instancetype)init {
    self = [super init];
    if (!self) return nil;
    
    [self addSubview:self.titleLbl];
    [self addSubview:self.contentLbl];
    [self addSubview:self.iconImgv];
    
    [_iconImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.width.height.mas_equalTo(35);
        make.centerY.equalTo(self);
    }];
    
    return self;
}

- (UILabel *)titleLbl {
    if (!_titleLbl) {
        _titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 100, 15)];
        _titleLbl.font = kCX_FONT(10);
        _titleLbl.text = @"自媒体";
    }
    return _titleLbl;
}

- (UILabel *)contentLbl {
    if (!_contentLbl) {
        _contentLbl = [[UILabel alloc] initWithFrame:CGRectMake(12, 50, 120, 15)];
        _contentLbl.font = kCX_FONT(6.5);
        _contentLbl.textColor = [UIColor lightGrayColor];
    }
    return _contentLbl;
}

- (UIImageView *)iconImgv {
    if (!_iconImgv) {
        _iconImgv = [[UIImageView alloc] init];
        _iconImgv.backgroundColor = [UIColor orangeColor];
    }
    return _iconImgv;
}


@end

@interface WeHomePageInterfaceCell ()

@property (nonatomic, strong) WeHomePageInterfaceControl *ctrl1;
@property (nonatomic, strong) WeHomePageInterfaceControl *ctrl2;
@property (nonatomic, strong) WeHomePageInterfaceControl *ctrl3;
@property (nonatomic, strong) WeHomePageInterfaceControl *ctrl4;

@end

@implementation WeHomePageInterfaceCell

#pragma mark - 子视图
- (void)subviewsInContentView {
    if (!_ctrl1 && !_ctrl2 && !_ctrl3 && !_ctrl4) {
        
        _ctrl1 = [self getCtrlWithTitle:@"自媒体" withContent:@"软文写作、公众号、网红" withImageName:@"" withIndex:0];
        _ctrl2 = [self getCtrlWithTitle:@"影音植入" withContent:@"影视植入、电视播放、综艺赞助" withImageName:@"" withIndex:1];
        _ctrl3 = [self getCtrlWithTitle:@"网络广告" withContent:@"网站广告位、SEO优化、搜索引擎" withImageName:@"" withIndex:2];
        _ctrl4 = [self getCtrlWithTitle:@"户外广告" withContent:@"电梯广告、户外墙广告、告诉公路广告" withImageName:@"" withIndex:3];

        [self.contentView addSubview:_ctrl1];
        [self.contentView addSubview:_ctrl2];
        [self.contentView addSubview:_ctrl3];
        [self.contentView addSubview:_ctrl4];
    }
}

- (WeHomePageInterfaceControl *)getCtrlWithTitle:(NSString *)title withContent:(NSString *)content withImageName:(NSString *)imgStr withIndex:(NSUInteger)index {
    WeHomePageInterfaceControl *ctrl = [[WeHomePageInterfaceControl alloc] init];
    CGFloat width = (kSCREEN_WIDTH-1)/2.f;
    CGFloat height = 164/2.f;
    ctrl.frame = (CGRect){index%2*(width+0.5),index/2*(height+0.5),width,height};
    ctrl.titleLbl.text = title;
    ctrl.contentLbl.text = content;
    ctrl.iconImgv.image = kIMAGE(imgStr);
    ctrl.backgroundColor = [UIColor whiteColor];
    ctrl.tag = 1211+index;
    [ctrl addTarget:self action:@selector(ctrlClickAction:) forControlEvents:UIControlEventTouchUpInside];
    return ctrl;
}

#pragma mark - 点击事件
- (void)ctrlClickAction:(WeHomePageInterfaceControl *)ctrl {
    WeHomePageInterfaceControlType type = WeHomePageInterfaceControlTypeMedia;
    
    switch (ctrl.tag-1211) {
        case 0:
            type = WeHomePageInterfaceControlTypeMedia;
            break;
        case 1:
            type = WeHomePageInterfaceControlTypeFilm;
            break;
        case 2:
            type = WeHomePageInterfaceControlTypeNet;
            break;
        case 3:
            type = WeHomePageInterfaceControlTypeOutdoor;
            break;
    }
    
    _ctrlClickBlock(type);
}


#pragma mark - 初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (!self) return nil;
    
    self.contentView.backgroundColor = [UIColor lightGrayColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self subviewsInContentView];
        
    return self;
}


@end
